<template>
    <div class="layout-container">
        <div class="layout-container-left">
            <UseTable :tableData="tableData" custom :labels="labels" @change="change" />
        </div>

        <div class="layout-container-right">
            <div class="form-box">
                <el-form label-width="auto">
                    <el-form-item>
                        <div class="data-select">
                            <DmData @dataChange="dataChange" />
                            <Upload @uploadSuccess="uploadSuccess" />
                        </div>
                    </el-form-item>

                    <el-form-item>
                        <el-button type="primary" @click="handleCalculate">计算</el-button>
                    </el-form-item>
                </el-form>
            </div>

            <el-divider />

            <div class="info-box">
                <h5>算法说明</h5>
                <p>假设自变量（时间或其他变量）与因变量呈线性关系，通过最小二乘法拟合直线 y=kx+b ，其中 k 为斜率， b 为截距。</p>

                <h5>使用范围</h5>

                <ul>
                    <li>数据呈现明显线性趋势（如人口随时间的稳步增长）；</li>
                    <li>分析两个变量间的相关性（如广告投入与销售额）；</li>
                    <li>适用于简单、稳定的趋势预测。</li>
                </ul>
            </div>
        </div>

        <Result v-model="resultShow" :result="resultData" />
    </div>
</template>

<script setup lang="ts">
import {onActivated, onMounted, ref} from "vue"
import {ElMessage}                   from "element-plus"
import Result                        from "./components/Result.vue"
import {getExcel}                    from "@/api/data"
import Upload                        from "@/components/Upload.vue"
import UseTable                      from "@/components/UseTable.vue"
import DmData                        from "@/components/DmData.vue"

function dataChange(data) {
    tableData.value = data
}

function uploadSuccess(res) {
    tableData.value = res.data
}

let labels = ["自变量", "因变量"]

function change(list) {
    paramsData.value.x = list?.find(item => item.label === "自变量")?.value || []
    paramsData.value.y = list?.find(item => item.label === "因变量")?.value || []
}

let paramsData = ref({
    x: [],
    y: [],
})

let resultShow = ref(false)
let resultData = ref("")

// 计算
function handleCalculate() {
    if (paramsData.value.x.length === 0) {
        ElMessage.warning("请选择一列作为自变量")
        return
    }

    if (paramsData.value.y.length === 0) {
        ElMessage.warning("请选择一列作为因变量")
        return
    }

    resultData.value = paramsData.value
    resultShow.value = true
}

let loading   = ref(true)
let tableData = ref([])

let getTableData = () => {
    loading.value = true

    getExcel("linear-regression").then(res => res.data).then(data => tableData.value = data)

    loading.value = false
}

/**
 * 初始化参数
 */
function initParam() {

}

// -----------------------------主程序start-----------------------------------------
onMounted(() => {
    initParam()
    getTableData()
})

onActivated(() => {
    initParam()
    getTableData()
})
// -----------------------------主程序end-----------------------------------------

</script>

<style scoped>

</style>

